package com.dundun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dundun.annotation.AuthCheck;
import com.dundun.common.utils.Ajax;
import com.dundun.domain.Collect;
import com.dundun.domain.Internship;
import com.dundun.domain.JobRelationship;
import com.dundun.service.CollectService;
import com.dundun.service.InternshipService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Api(tags = "收藏相关接口")
@RequestMapping("/")
@RestController
public class CollectController {
    @Resource
    private CollectService collectService;
    @Resource
    private InternshipService internshipService;
    @ApiOperation(value = "用户收藏")
    @PostMapping("/PutCollection")
    @AuthCheck
    public Ajax PutCollection(int internshipId, int userId) {
        Collect collect = new Collect();
        collect.setInternshipId((long) internshipId);
        collect.setUserId((long) userId);
        collect.setCollectedTime(new Date());
        if (collectService.save(collect)){
            return Ajax.success();
        }
        return Ajax.error("收藏失败，请重试");
    }

    @ApiOperation(value = "取消用户收藏")
    @PostMapping("/PutCancelCollection")
    @AuthCheck
    public Ajax PutCancelCollection(int internshipId, int userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("internship_id", internshipId);
        map.put("user_id", userId);
        if (collectService.removeByMap(map)){
            return Ajax.success();
        }
        return Ajax.error("取消收藏失败，请重试");
    }

    @ApiOperation(value = "获取用户收藏的岗位列表")
    @GetMapping("/GetMyCollection")
    @AuthCheck
    public Ajax GetMyCollection(int userId) {
        List<Collect> collectList = collectService.list(
                new QueryWrapper<Collect>().eq("user_id", userId)
        );
        ArrayList<Internship> internshipList = new ArrayList<>();
        collectList.forEach(b -> internshipList.add(internshipService.getById(b.getInternshipId())));
        return Ajax.success().put("data",internshipList);
    }

    @ApiOperation(value = "分页获取用户收藏的岗位列表")
    @GetMapping("/GetMyCollectionByPage")
    public Ajax GetMyCollectionByPage(int userId) {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "判断用户是否收藏")
    @GetMapping("/GetCollected")
    @AuthCheck
    public Ajax GetCollected(int userId,int internshipId) {
        Map<String , Object> param = new HashMap<>();
        param.put("user_id" , userId);
        param.put("internship_id" , internshipId);
        List<Collect> collectList = collectService.list(
                new QueryWrapper<Collect>().allEq(param)
        );
        System.out.println(collectList.size());
        if (collectList.size() > 0){
            return Ajax.success().put("hasCollected",true);
        }
        return Ajax.success().put("hasCollected",false);
    }

}
